<?php

namespace plugin\coupon\app\model;

use app\model\user\User;
use think\Model;

/**
 * t_coupon_receive_log 优惠券领取详情
 * @property integer $id id(主键)
 * @property string $code 优惠券码
 * @property integer $coupon_id 优惠券id
 * @property string $coupon_name 优惠券名称
 * @property integer $order_id 关联订单的id
 * @property integer $user_id 领取人id
 * @property string $user_name 领取人名
 * @property integer $status 状态 1:未使用 2:已使用 3:已过期
 * @property string $used_time 使用时间
 * @property string $start_time 有效期开始
 * @property string $end_time 有效期结束
 * @property string $create_time 创建时间
 * @property string $update_time 更新时间
 */
class CouponReceiveLog extends Model
{
    /**
     * The connection name for the model.
     *
     * @var string|null
     */
    protected $connection = 'mysql';

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 't_coupon_receive_log';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $pk = 'id';

    const STATUS_NO_USED = 1;
    const STATUS_USED = 2;
    const STATUS_EXPIRE = 3;
    const STATUS_TEXT_MAP = [
        self::STATUS_NO_USED => "未使用",
        self::STATUS_USED => "已使用",
        self::STATUS_EXPIRE => "已过期",
    ];

    protected $append = [
        'status_text',
    ];

    public function getStatusTextAttr($value, $data)
    {
        return self::STATUS_TEXT_MAP[$data['status']] ?? "";
    }

    public function coupon()
    {
        return $this->belongsTo(Coupon::class, 'coupon_id', 'id');
    }

    public function coupongoods()
    {
        return $this->hasMany(CouponGoods::class, 'coupon_id', 'coupon_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }



}
